//
//  SNAddCommentsDataManager.m
//  app
//
//  Created by 王少宁 on 15/5/19.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "SNAddCommentsDataManager.h"

@interface SNAddCommentsDataManager ()
@property (nonatomic, retain) JKHttpClient * http;
@property (nonatomic, retain) SNUniteUrl * uniteUrl;
@end

@implementation SNAddCommentsDataManager

+ (SNAddCommentsDataManager *)shareManager
{
    AS_SINGLETON(SNAddCommentsDataManager, manager, {
        manager = [[SNAddCommentsDataManager alloc] init];
        manager.uniteUrl = [SNUniteUrl shareUniteUrl];
        manager.http = [[JKHttpClient alloc] init];
    })
    return manager;
}

//上传文字
- (void)addcommentsTextWithResult:(SNAddCommentsUploadTextHttpResult *)result sn:(NSString *)sn productId:(NSString *)productId contents:(NSString *)contents pics:(NSString *)pics scores:(int)scores
{
    if ([self.http isRunning]) {
        return;
    }
    
    result.failure = YES;
    result.success = NO;
    
    NSDictionary * params = [[NSDictionary alloc] init];
    
    if (pics.length > 0) {//有图片
        params = @{@"sn":sn,
                   @"product_id":productId,
                   @"contents":contents,
                   @"pics":pics,
                   @"scores":[NSString stringWithFormat:@"%d",scores]};
    } else {//没有图片
        params = @{@"sn":sn,
                   @"product_id":productId,
                   @"contents":contents,
                   @"scores":[NSString stringWithFormat:@"%d",scores]};
    }
    
    
    [self.http post:self.uniteUrl.sbpd_addCommentTextUrl params:params result:^(JKResponse *resp) {
        if (resp.OK) {
            result.success = YES;
            result.failure = NO;
        } else if (resp.failure) {
            result.failure = YES;
            result.success = NO;
        }
        [result invoke];
    }];
}

@end




@interface SNAddCommentsUpLoadPicDataManager ()
@property (nonatomic, retain) SNUniteUrl * uniteUrl;
@property (nonatomic, retain) JKHttpClient * http;
@end

@implementation SNAddCommentsUpLoadPicDataManager

+ (SNAddCommentsUpLoadPicDataManager *)shareManager
{
    AS_SINGLETON(SNAddCommentsUpLoadPicDataManager, manager, {
        manager = [[SNAddCommentsUpLoadPicDataManager alloc] init];
        manager.uniteUrl = [SNUniteUrl shareUniteUrl];
        manager.http = [[JKHttpClient alloc] init];
    })
    return manager;
}

//上传图片
- (void)addcommentsPicWithResult:(SNAddCommentsUploadImgHttpResult *)result params:(NSDictionary *)params imageData:(NSData *)image
{
    //    if ([self.http isRunning]) {
    //        return;
    //    }
    
    result.failure = YES;
    result.success = NO;
    
#warning - 测试地址 @"http://mcpm.kangxihui.com/uploads/qaddcommentspic"
    
    [self.http upload:self.uniteUrl.sbpm_addCommentPicUrl data:image params:params result:^(JKResponse *resp) {
        DLog(@"%@",resp.request.URL);
        DLog(@"%@   %@",resp.message,resp.content);
        if (resp.OK) {
            result.success = YES;
            result.failure = NO;
            
            NSString * imageStr = resp.content[@"image"];
            int height = [resp.content[@"height"] intValue];
            int width = [resp.content[@"width"] intValue];
            
            NSString * uploadString = [NSString stringWithFormat:@"%@,%d,%d",imageStr,height,width];
            DLog(@"%@",resp.content[@"uploadString"]);
            [result.picsArray addObject:uploadString];
            
        } else if (resp.failure) {
            result.failure = YES;
            result.success = NO;
        }
        [result invoke];
    }];
}

@end

